package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.mapper.TbRegionMapper;
import com.heima.pojo.TbNode;
import com.heima.mapper.TbNodeMapper;
import com.heima.pojo.dto.TbNodeDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.TbNodeSearchVo;
import com.heima.pojo.vo.TbNodeVo;
import com.heima.pojo.vo.TbVendingMachineVo;
import com.heima.service.ITbNodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 点位表 服务实现类
 * </p>
 *
 * @author AkiRinko
 * @since 2024-06-08
 */
@Service
@RequiredArgsConstructor
public class TbNodeServiceImpl extends ServiceImpl<TbNodeMapper, TbNode> implements ITbNodeService {
    private final TbNodeMapper tbNodeMapper;

    /**
     * 点位搜索/区域详情
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     * @param regionId
     * @return
     */
    @Override
    public PageResponse<TbNodeSearchVo> selectNodePage(Integer pageIndex, Integer pageSize, String name, String regionId) {
        Page<TbNodeSearchVo> page = new Page<>(pageIndex, pageSize);
        Page<TbNodeSearchVo> result = tbNodeMapper.selectNodePage(name, regionId, page);

        return PageResponse.of(result, TbNodeSearchVo.class);
    }

    /**
     * 新增点位
     *
     * @param tbNodeDto
     */
    @Override
    public void insertNode(TbNodeDto tbNodeDto) {
        TbNode tbNode = BeanUtil.copyProperties(tbNodeDto, TbNode.class);
        tbNode.setCreateTime(LocalDateTime.now());
        tbNode.setUpdateTime(LocalDateTime.now());
        tbNodeMapper.insert(tbNode);
    }

    /**
     * 获取点位详情
     *
     * @param nodeId
     * @return
     */
    @Override
    public List<TbVendingMachineVo> selectVMList(String nodeId) {
        return tbNodeMapper.selectVMList(nodeId);
    }

    /**
     * 修改点位
     *
     * @param nodeId
     * @param dto
     */
    @Override
    public void updateNode(String nodeId, TbNodeDto dto) {
        TbNode tbNode = BeanUtil.copyProperties(dto, TbNode.class);

        LambdaQueryWrapper<TbNode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbNode::getId, nodeId);

        tbNodeMapper.update(tbNode, wrapper);
    }

    /**
     * 删除点位
     *
     * @param nodeId
     */
    @Override
    public void deleteNode(String nodeId) {
        tbNodeMapper.deleteById(nodeId);
    }

    /**
     * 点位数量
     */
    @Override
    public Integer nodeCount() {
        return count();
    }
}
